package com.ymttest.business.service;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import com.ymt.core.tool.Logger;

public class ActivityPoolService {
	private  static Map<Integer,Integer> activityPools = new HashMap<>();
	
	public synchronized static void removeActivityIdFromPool(int activityId){
		Set<Integer> actTplIdList = activityPools.keySet().stream().filter(k->activityPools.get(k).equals(activityId)).collect(Collectors.toSet());
		actTplIdList.forEach(x->{
			activityPools.remove(x);
			Logger.comment("移出活动从活动池, ActivityTemplateId:"+x+", ActivityId:"+activityId);
			
		});
	}
	
	public synchronized static void setActivityFromPool(int activityTemplateId,int activityId){
		activityPools.put(activityTemplateId, activityId);
		Logger.comment("添加活动到活动池, ActivityTemplateId:"+activityTemplateId+", ActivityId:"+activityId);
	}
	
	public  static int getActivityFromPool(Integer activityTemplate){
		if (activityPools.keySet().contains(activityTemplate)){
			return activityPools.get(activityTemplate);
		}else{
			return 0;
		}
	}
	
	
	
	//public static ThreadPoolExecutor pool = new ThreadPoolExecutor(1  , 2 , Long.MAX_VALUE, TimeUnit.SECONDS,new LinkedBlockingQueue<>());
	
	
	
	public static void main(String args[]) throws InterruptedException{
		
	}
	
	
	
	
    @SuppressWarnings("unchecked")
	public static <T> T clone(T obj) throws Exception {
    	ByteArrayOutputStream baos = new ByteArrayOutputStream();
    	ObjectOutputStream oos = new ObjectOutputStream(baos);
    	oos.writeObject(obj);
    	
    	ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    	ObjectInputStream ois = new ObjectInputStream(bais);
    	return (T)ois.readObject();
    	
    	
    
    
	
    }
	
	
	
	
	
	
	
	
	
}
